﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

using Bpms.Data;
using Bpms.Log;
using Bpms.Config;
using Bpms.Entity;
using Bpms.Cache;

namespace Bpms.Bll
{
    public class BusinessType
    {
        #region Member
        /// <summary>
        /// 数据库改动事件委托
        /// </summary>
        public delegate void DBChangedEventHandler();

        /// <summary>
        /// 日志事件委托
        /// </summary>
        public delegate void LogEventHandler();

        /// <summary>
        /// 数据库改动事件
        /// </summary>
        public static event DBChangedEventHandler DBChanged;

        /// <summary>
        /// 写日志事件
        /// </summary>
        public static event LogEventHandler WriteLog;

        #region Private
        static IDataProvider provider = DatabaseProvider.GetInstance();
        static CacheContext cacheProvider = CacheContext.GetCacheService(new DefaultCache());

        static BusinessType()
        {
            //给数据库操作类设置日志适配器
            provider.SetLog(new ExceptionLogAdapter());
        }

        /// <summary>
        /// 取匹配率缓存
        /// </summary>
        /// <returns></returns>
        static IList<MatchRateInfo> GetMatchRateCache()
        {
            return cacheProvider.RetrieveObject(CacheKey.MatchRateList.ToString()) as IList<MatchRateInfo>;
        }

        /// <summary>
        /// 添加匹配率缓存
        /// </summary>
        /// <param name="cache"></param>
        static void AddMatchRateCache(object cache)
        {
            cacheProvider.AddObject(CacheKey.MatchRateList.ToString(), cache);
        }

        /// <summary>
        /// 取业务类型缓存
        /// </summary>
        /// <returns></returns>
        static IList<BusinessTypeInfo> GetBusinessTypeCache()
        {
            return cacheProvider.RetrieveObject(CacheKey.BusinessTypeList.ToString()) as IList<BusinessTypeInfo>;
        }

        /// <summary>
        /// 添加业务类型缓存
        /// </summary>
        /// <param name="cache"></param>
        static void AddBusinessTypeCache(object cache)
        {
            cacheProvider.AddObject(CacheKey.BusinessTypeList.ToString(), cache);
        }

        /// <summary>
        /// 取业务类型扩展缓存
        /// </summary>
        /// <returns></returns>
        static IList<BusinessTypeExtensionInfo> GetBusinessTypeExtensionCache()
        {
            return cacheProvider.RetrieveObject(CacheKey.BusinessTypeExtensionList.ToString()) as IList<BusinessTypeExtensionInfo>;
        }

        /// <summary>
        /// 添加业务类型扩展缓存
        /// </summary>
        /// <param name="cache"></param>
        static void AddBusinessTypeExtensionCache(object cache)
        {
            cacheProvider.AddObject(CacheKey.BusinessTypeExtensionList.ToString(), cache);
        }

        /// <summary>
        /// 数据改动事件处理函数
        /// </summary>
        static void OnDBChanged()
        {
            if (DBChanged != null)
            {
                DBChanged();
            }
        }

        /// <summary>
        /// 写日志处理函数
        /// </summary>
        static void OnWriteLog()
        {
            if (WriteLog != null)
            {
                WriteLog();
            }
        }
        #endregion

        #endregion

        /// <summary>
        /// 添加业务类型
        /// </summary>
        /// <param name="businessType">业务类型实例</param>
        /// <param name="businessTypeExtensions">业务类型扩展实例</param>
        /// <returns>-1=存在相同记录；0=添加失败；1=添加成功</returns>
        public static int Add(BusinessTypeInfo businessType, BusinessTypeExtensionInfo[] businessTypeExtensions)
        {
            if (provider.BusinessTypeExists(businessType, OperateType.Insert))
            {
                return -1;
            }

            if (provider.InsertBusinessType(businessType, businessTypeExtensions))
            {
                OnDBChanged();
                OnWriteLog();
                return 1;
            }

            return 0;
        }

        /// <summary>
        /// 更新业务类型
        /// </summary>
        /// <param name="businessType">业务类型</param>
        /// <param name="businessTypeExtensions">业务类型扩展</param>
        /// <returns>-1=存在相同记录；0=添加失败；1=添加成功</returns>
        public static int Update(BusinessTypeInfo businessType, BusinessTypeExtensionInfo[] businessTypeExtensions)
        {
            if (provider.BusinessTypeExists(businessType, OperateType.Update))
            {
                return -1;
            }

            if (provider.UpdateBusinessType(businessType, businessTypeExtensions))
            {
                OnDBChanged();
                OnWriteLog();
                return 1;
            }

            return 0;
        }

        /// <summary>
        /// 删除业务类型
        /// </summary>
        /// <param name="businessTypeId">业务类型ID</param>
        /// <returns></returns>
        public static bool Delete(int businessTypeId)
        {
            if (provider.DeleteBusinessType(businessTypeId))
            {
                OnDBChanged();
                OnWriteLog();
                return true;
            }

            return false;
        }

        /// <summary>
        /// 返回信息
        /// </summary>
        /// <param name="businessTypeId">业务类型ID</param>
        /// <returns></returns>
        public static BusinessTypeInfo GetInfo(int businessTypeId)
        {
            var cache = GetBusinessTypeCache();

            if (cache == null)
            {
                return provider.GetBusinessType(businessTypeId);
            }

            var result = from cacheItem in cache
                         where cacheItem.ID == businessTypeId
                         select cacheItem;

            if (result.Count() > 0)
            {
                return result.First() as BusinessTypeInfo;
            }

            return null;
        }

        /// <summary>
        /// 返回信息列表
        /// </summary>
        /// <param name="businessCategoryId">业务类别ID</param>
        /// <returns></returns>
        public static IList<BusinessTypeInfo> GetList(int businessCategoryId)
        {
            var cache = GetBusinessTypeCache();

            if (cache == null)
            {
                cache = provider.GetAllBusinessTypes();
                AddBusinessTypeCache(cache);
            }

            var result = from cacheItem in cache
                         where cacheItem.CategoryID == businessCategoryId
                         select cacheItem;

            var list = new List<BusinessTypeInfo>();

            if (result != null)
            {
                foreach (var item in result)
                {
                    list.Add(item);
                }
            }

            return list;
        }

        /// <summary>
        /// 返回单个扩展信息
        /// </summary>
        /// <param name="businessTypeExtensionId">扩展信息ID</param>
        /// <returns></returns>
        public static BusinessTypeExtensionInfo GetExtensionInfo(int businessTypeExtensionId)
        {
            var cache = GetBusinessTypeExtensionCache();

            if (cache == null)
            {
                return provider.GetBusinessTypeExtension(businessTypeExtensionId);
            }

            var result = from cacheItem in cache
                         where cacheItem.ID == businessTypeExtensionId
                         select cacheItem;

            if (result.Count() > 0)
            {
                return result.First() as BusinessTypeExtensionInfo;
            }

            return null;
        }

        /// <summary>
        /// 返回扩展信息列表
        /// </summary>
        /// <param name="businessTypeId">业务类型ID</param>
        /// <returns></returns>
        public static IList<BusinessTypeExtensionInfo> GetExtensionInfos(int businessTypeId)
        {
            var cache = GetBusinessTypeExtensionCache();

            if (cache == null)
            {
                cache = provider.GetAllBusinessTypeExtensions();
                AddBusinessTypeExtensionCache(cache);
            }

            var result = from cacheItem in cache
                         where cacheItem.BusinessTypeID == businessTypeId
                         select cacheItem;

            var list = new List<BusinessTypeExtensionInfo>();

            if (result != null)
            {
                foreach (var item in result)
                {
                    list.Add(item);
                }
            }

            return list;
        }

        /// <summary>
        /// 设置业务类型显示顺序
        /// </summary>
        /// <param name="businessTypeId">业务类型ID</param>
        /// <param name="sortNumber">显示顺序</param>
        /// <returns>-1=sortNumber不在接受的范围内；0=设置失败；1=设置成功</returns>
        public static int SetSortNumber(int businessTypeId, int sortNumber)
        {
            if (!provider.IsBusinessTypeSortable((byte)sortNumber))
            {
                return -1;
            }
            if (provider.SetTypeSortNumber(businessTypeId, sortNumber))
            {
                OnDBChanged();
                OnWriteLog();
                return 1;
            }

            return 0;
        }

        /// <summary>
        /// 返回匹配率信息
        /// </summary>
        /// <param name="id">匹配率ID</param>
        /// <returns></returns>
        public static MatchRateInfo GetMatchRate(int id)
        {
            var cache = GetMatchRateCache();

            if (cache == null)
            {
                return provider.GetMatchRate(id);
            }

            var result = from cacheItem in cache
                         where cacheItem.ID == id
                         select cacheItem;

            if (result.Count() > 0)
            {
                return result.First() as MatchRateInfo;
            }

            return null;
        }

        /// <summary>
        /// 返回匹配率类表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IList<MatchRateInfo> GetMatchRates(int type)
        {
            var cache = GetMatchRateCache();

            if (cache == null)
            {
                cache = provider.GetAllMatchRates();
                AddMatchRateCache(cache);
            }

            var result = from cacheItem in cache
                         where cacheItem.Type == (byte)type
                         select cacheItem;

            var list = new List<MatchRateInfo>();

            if (result != null)
            {
                foreach (var item in result)
                {
                    list.Add(item);
                }
            }

            return list;
        }
    }
}
